/**
 * 
 */
package cn.nawang.ebeim.api.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.nawang.ebeim.api.comm.PageList;
import cn.nawang.ebeim.api.comm.Pages;
import cn.nawang.ebeim.api.comm.SearchFormParams;
import cn.nawang.ebeim.api.domain.BackupLog;
import cn.nawang.ebeim.api.domain.BackupLogWrapper;
import cn.nawang.ebeim.api.service.BackupLogService;
import cn.nawang.ebeim.api.utils.Constant;

/**
 * @ClassName: BackupLogController
 * @Description: 备份日志控制器
 * @author 张建峰
 * @date 2015年8月7日15:09:58
 * @version v1.0
 */
@RestController
public class BackupLogController {
  private static final Logger LOG = LoggerFactory.getLogger(BackupLogController.class);

  @Autowired
  private BackupLogService backupLogService;

  /**
   * @author 张建峰
   * @Description: 分页查询
   * @param @param backupLog
   * @param @param page
   * @param @return
   * @return Object
   * @date 2015年9月22日下午2:54:05
   *
   * @修改人 张建峰
   * @修改日期
   */
  @RequestMapping(value = "/backuplog/pages/list", method = RequestMethod.GET)
  public Object findByPage(BackupLog backupLog, Pages page) {
    LOG.debug("/backuplog/pages/list {}", backupLog, page);
    Map<String, Object> map = new HashMap<String, Object>();
    PageList list = backupLogService.findByPage(backupLog, page);
    if (list != null) {
      map.put(Constant.JSON_CODE, Constant.CODE_200);
      map.put("list", list);
    } else {
      map.put(Constant.JSON_CODE, Constant.CODE_404);
    }
    LOG.debug("findByPageBackupLog {}", map);
    return map;
  }

  /**
   * @author 张建峰
   * @Description: 多条件查询
   * @param @param backupLog
   * @param @return
   * @return Object
   * @date 2015年9月22日下午2:54:05
   *
   * @修改人 张建峰
   * @修改日期
   */
  @RequestMapping(value = "/token/backuplog/list", method = RequestMethod.GET)
  public Object findByBackupLog(BackupLog backupLog) {
    LOG.debug("/token/backuplog/list {}", backupLog);
    Map<String, Object> map = new HashMap<String, Object>();
    List<BackupLog> list = backupLogService.findByBackupLog(backupLog);
    if (list != null) {
      map.put(Constant.JSON_CODE, Constant.CODE_200);
      map.put("list", list);
    } else {
      map.put(Constant.JSON_CODE, Constant.CODE_404);
    }
    LOG.debug("findByBackupLog {}", map);
    return map;
  }

  /**
   * @author 张建峰
   * @Description: 新增
   * @param @param backupLog
   * @param @return
   * @return Object
   * @date 2015年9月22日下午2:54:05
   *
   * @修改人 张建峰
   * @修改日期
   */
  @RequestMapping(value = "/token/backuplog/add", method = RequestMethod.POST)
  public Object add(BackupLog backupLog) {
    LOG.debug("/token/backuplog/add {}", backupLog);
    Map<String, Object> map = new HashMap<String, Object>();
    backupLogService.save(backupLog);
    map.put(Constant.JSON_CODE, Constant.CODE_200);
    LOG.debug("addBackuplog {}", map);
    return map;
  }

  /**
   * @author 张建峰
   * @Description: 更新
   * @param @param backupLog
   * @param @return
   * @return Object
   * @date 2015年9月22日下午2:54:05
   *
   * @修改人 张建峰
   * @修改日期
   */
  @RequestMapping(value = "/token/backuplog/updata", method = RequestMethod.POST)
  public Object updata(BackupLog backupLog) {
    LOG.debug("/token/backuplog/updata {}", backupLog);
    Map<String, Object> map = new HashMap<String, Object>();
    backupLogService.update(backupLog);
    map.put(Constant.JSON_CODE, Constant.CODE_200);
    LOG.debug("updateBackupLog {}", map);
    return map;
  }

  @RequestMapping(value = "/token/backuplog/delete", method = RequestMethod.GET)
  public Object deleteByProperty(BackupLog backupLog) {
    LOG.debug("/token/backuplog/delete {}", backupLog);
    Map<String, Object> map = new HashMap<String, Object>();
    backupLogService.delete(backupLog);
    map.put(Constant.JSON_CODE, Constant.CODE_200);
    LOG.debug("deleteByProperty {}", map);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 根据ds_id获取列表 并对end_time进行排序，时间越大，排越前。
   * @param backupLog
   * @return List<BackupLog>
   * @date Sep 29, 20159:04:12 AM
   *
   * @修改人 张建峰
   * @修改日期 2015年9月30日10:36:12
   */
  @RequestMapping(value = "/token/backuplog/find/dsid", method = RequestMethod.POST)
  public Map<String, Object> findByDsId(@RequestBody BackupLogWrapper backupLogWrapper,
      HttpServletRequest request) {
    LOG.debug("findByDsIdAndStatus {}", backupLogWrapper);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("code", Constant.CODE_ERROR);
    String dsId = backupLogWrapper.getBackupLog().getDsId();
    List<SearchFormParams> dtovect = new ArrayList<SearchFormParams>();
    if (!StringUtils.isEmpty(dsId)) {
      dtovect.add(new SearchFormParams("bl.dsId", dsId, "dsId", SearchFormParams.STRING_TYPE));
    }
    Integer status = backupLogWrapper.getBackupLog().getStatus();
    if(status!=null){
      dtovect.add(new SearchFormParams("bl.status", status, "status", SearchFormParams.INTEGER_TYPE));
    }
    Pages pages = backupLogWrapper.getPages();
    if (dtovect.size() > 0) {
      pages.setSearchFormParams(dtovect.toArray(new SearchFormParams[dtovect.size() - 1]));
    }
    PageList list = backupLogService.findByDsIdAndStatus(pages);
    map.put("list", list);
    map.put("code", Constant.CODE_SUCCESS);
    LOG.debug("findByDsId {}", map);
    return map;
  }
}
